﻿using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;

[XmlRoot("Order")]
public class Order
{
    [XmlElement("OrderId")]
    public int OrderId { get; set; }

    [XmlElement("CustomerName")]
    public string CustomerName { get; set; }

    [XmlElement("TotalAmount")]
    public decimal TotalAmount { get; set; }

}

public class OrderService
{
    // 导出订单列表到 XML 文件
    public void Export(string filePath, List<Order> orders)
    {
        try
        {
         
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (StreamWriter writer = new StreamWriter(filePath))
            {
            
                serializer.Serialize(writer, orders);
            }

            Console.WriteLine("Orders have been successfully exported to XML.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error exporting orders: {ex.Message}");
        }
    }

    // 从 XML 文件导入订单列表
    public List<Order> Import(string filePath)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));

            using (StreamReader reader = new StreamReader(filePath))
            {
                return (List<Order>)serializer.Deserialize(reader);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error importing orders: {ex.Message}");
            return null;
        }
    }
}

class Program
{
    static void Main()
    {
        OrderService orderService = new OrderService();

        List<Order> orders = new List<Order>
        {
            new Order { OrderId = 1, CustomerName = "John Doe", TotalAmount = 100.50m },
            new Order { OrderId = 2, CustomerName = "Jane Smith", TotalAmount = 250.75m }
        };

        // 导出订单列表到 XML 文件
        string exportFilePath = "orders.xml";
        orderService.Export(exportFilePath, orders);

        // 从 XML 文件导入订单列表
        List<Order> importedOrders = orderService.Import(exportFilePath);

        // 输出导入的订单
        if (importedOrders != null)
        {
            Console.WriteLine("Imported Orders:");
            foreach (var order in importedOrders)
            {
                Console.WriteLine($"OrderId: {order.OrderId}, Customer: {order.CustomerName}, TotalAmount: {order.TotalAmount}");
            }
        }
    }
}